package com.sykj.bsc.cl.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sykj.bsc.cl.bean.Bis_shop_custom;
import com.sykj.bsc.hyx.service.Bis_opinionService;
import com.sykj.bsc.jhy.entity.Bis_business_order;
import com.sykj.bsc.jhy.entity.Bis_nurse_project;
import com.sykj.bsc.jhy.entity.Bis_order_deposit;
import com.sykj.bsc.jhy.entity.Bis_project_record;
import com.sykj.bsc.jhy.service.serviceImpl.Bis_nurse_projectServiceImpl;
import com.sykj.bsc.wsy.model.Bis_beauty_config;
import com.sykj.bsc.wsy.model.Bis_coupon_record;
import com.sykj.bsc.wsy.service.Bis_beauty_configService;
import com.sykj.bsc.zjj.bean.Bis_custom_user;
import com.sykj.bsc.zjj.service.Bis_shop_userService;
import com.sykj.bsc.zn.bean.Bis_beauty_record;
import com.sykj.bsc.zn.bean.Bis_shop;
import com.sykj.bsc.zn.service.znServiceImpl;
/**
 * 三级菜单和一些二级
 * @author Lenovo
 *
 */
@Controller
@RequestMapping("wechat")
public class ComprehensiveController {
	//王舒雅 wsy
	@Autowired
	Bis_beauty_configService service;
	//张俊杰 zjj
	@Autowired
	private Bis_shop_userService bsus;
	//江海阳 jhy
	@Autowired 
	Bis_nurse_projectServiceImpl impl;
	//张诺 zn
	@Autowired
	znServiceImpl sus;
	//胡颖箫hyx
	@Autowired
	public Bis_opinionService bis_opinionService;
	
	/**
	 * 颜值币
	 * @param model
	 * @param BBC
	 * @return
	 */
	//用来购买颜值币的页面
	@RequestMapping("/GobuyBeauty")
	public String GobuyBeauty(Model model,Bis_beauty_config BBC){
		model.addAttribute("BBCList", service.selectBis_beauty_config(BBC));
		return "/backstage/cl/wechat/my/buyBeauty";
	}
	//跳转到用来兑换颜值币的界面
	@RequestMapping("/GoexchangeBeauty")
	public String GoexchangeBeauty(HttpServletRequest request){
		
		return "/backstage/cl/wechat/my/exchangeBeauty";
	}
	//兑换颜值
	@RequestMapping("/Beauty")
	@ResponseBody
	public String Beauty(HttpServletRequest request,HttpSession session){
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
		Map<String, Object> map =init(request);
		map.put("custom_user_id", ss.getCustom_user_id());
		//查询礼券兑换记录表
		Bis_coupon_record bis=service.selectBis_coupon_record(map);
		map.put("beauty_price", bis.getBis_coupon_active().getBeauty_num());
		if (bis.getStatus()==null || bis.getStatus().equals("")) {
			return "0";
		}else if (bis.getStatus()=="1" || bis.getStatus().equals("1") ) {
			//修改礼券兑换记录表的兑换状态
			this.service.updateBis_coupon_record(map);
			//修改用户的颜值
			this.bis_opinionService.updateBis_custom_user1(map);
			//修改活动表
			
			//向颜值流水信息表添加纪录
			//生成5位随机数
			int max=999999;
	        int min=10000;
	        Random random = new Random();
	        Integer ran = random.nextInt(max)%(max-min+1) + min;
	        String code1=ran.toString();
	        String code3="1706"+code1;
			map.put("record_id", code3);
			map.put("order_id", "");
			map.put("beauty_num", bis.getBis_coupon_active().getBeauty_num());
			map.put("record_type", "3");
			map.put("shop_id", "");
			this.bis_opinionService.saveBis_beauty_record(map);
			return "1";
		}else if (bis.getStatus()=="2" || bis.getStatus().equals("2") ) {
			return "2";
		}else if (bis.getStatus()=="3" || bis.getStatus().equals("3") ) {
			return "3";
		}else{
			System.out.println("444444");
			return "4";
		}
	}
	
	public Map<String,Object> init(HttpServletRequest request){
		Map<String,Object> map =new HashMap<>();
		String mobile=request.getParameter("mobile");
		String cdkey=request.getParameter("cdkey");
		System.out.println(mobile+"::::::::::::::::::::::::::::::::"+cdkey);
		map.put("mobile", mobile);
		map.put("cdkey", cdkey);
		return map;
	}
	
	/**
	 * 交易详情
	 * @param session
	 * @param model
	 * @return
	 */
	//跳转到用来显示交易详情的页面
	@RequestMapping("/GobeautyRecordList")
	public String GobeautyRecordList(HttpSession session,Model model){
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
		String custom_user_id=ss.getCustom_user_id();
		List<Bis_beauty_record> bis_beauty_record =bis_opinionService.getBis_beauty_recordByid(custom_user_id);
		model.addAttribute("bis_beauty_record", bis_beauty_record);
		return "/backstage/cl/wechat/my/beautyRecordList";
	}
	//将交易的详细信息显示出来
	@RequestMapping("/Gobeauty")
	@ResponseBody
	public List<Bis_beauty_record> Gobeauty(HttpSession session){
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
		String custom_user_id=ss.getCustom_user_id();
		List<Bis_beauty_record> bis_beauty_record =bis_opinionService.getBis_beauty_recordByid(custom_user_id);
		return bis_beauty_record;
	}
	
	
	
	
	/**
	 * 个人详细
	 * @param model
	 * @param session
	 * @return
	 */
	//跳转到用来显示个人详细的链接
	@RequestMapping("/GomyDetail")
	public String GomyDetail(Model model,HttpSession session){
		Bis_custom_user ss=(Bis_custom_user) session.getAttribute("user");
		Map<String, Object> map=new HashMap<>();
		map.put("custom_user_id", ss.getCustom_user_id());
		Bis_custom_user bis_custom_user=bsus.getMobileBis_custom_user(map);
		model.addAttribute("bis_custom_user", bis_custom_user);
		return "/backstage/cl/wechat/my/myDetail";
	}
	@RequestMapping("/updatemyDetail")
	@ResponseBody
	public String updatemyDetail(@RequestParam("custom_user_id")String custom_user_id,@RequestParam("nikename")String nikename,
			@RequestParam("born_date")String born_date,@RequestParam("sex")String sex){
		Map<String,Object> map=new HashMap<>();
		map.put("custom_user_id", custom_user_id);
		map.put("nikename", nikename);
		map.put("born_date", born_date);
		map.put("sex", sex);
		this.bsus.updateBis_custom_user(map);
		return "1";
	}
	
	/**
	 * 商品
	 * @param project_id
	 * @param model
	 * @return
	 */
	//跳转到用来显示商品的详细信息的链接
	@RequestMapping("/GoshowProjectDetail")
	public String GoshowProjectDetail(@RequestParam(value="project_id")String project_id,Model model){
		System.out.println("project_id:::::"+project_id);
		//根据编号获取商品的详细信息
		Bis_nurse_project bis=impl.getBis_nurse_projectId(project_id);
		model.addAttribute("bis", bis);
		return "/backstage/cl/wechat/project/showProjectDetail";
	}
	
	//跳转到用来预约商品信息的链接
	@RequestMapping("/GofindShop")
	public String GofindShop(@RequestParam(value="returnType")String returnType,@RequestParam(value="project_id")String project_id,Model model){
		if("1".equals(returnType)){
			model.addAttribute("project_id", project_id);
			return "/backstage/cl/wechat/project/selectShop";
		}else{
			model.addAttribute("project_id", project_id);
			return "/backstage/cl/wechat/shop/findShop";
		}
	}
	
	/**
	 * 店铺
	 * @param model
	 * @return
	 */
	//跳转到店铺的信息的链接
	@RequestMapping("/GoselectShop")
	@ResponseBody
	public List<Bis_shop> GoselectShop(Model model){
		List<Bis_shop> bis_shop=sus.selectBis_shop();
		return bis_shop;
	}
	
	//从店铺跳转到商品信息的链接  并切获取用户浏览店铺记录
	@RequestMapping("/GosubscribeProject")
	public String GosubscribeProject(Model model,@RequestParam(value="project_id")String project_id,@RequestParam(value="shop_id")String shop_id,HttpSession session){
		Bis_custom_user bis_custom_user=(Bis_custom_user) session.getAttribute("user");
		String custom_user_id=bis_custom_user.getCustom_user_id();
		//获取用户浏览店铺进行查询判断用户是否已经浏览过了
		//获取用户浏览店铺记录
		Map<String,Object> map=new HashMap<>();
		map.put("custom_user_id",custom_user_id);
		map.put("shop_id", shop_id);
		List<Bis_shop_custom> bis_shop_custom=bis_opinionService.selectBis_shop_custom(map);
		if (bis_shop_custom.size()==0) {
			this.bis_opinionService.saveBis_shop_custom(map);
		}else{
			this.bis_opinionService.updateBis_shop_custom(map);
		}
		//显示页面
		Bis_nurse_project bis_nurse_project=impl.getBis_nurse_projectId(project_id);
		model.addAttribute("bis_nurse_project", bis_nurse_project);
		Bis_shop bis_shop= sus.getBis_shopByid(shop_id);
		model.addAttribute("bis_shop", bis_shop);
		model.addAttribute("bis_custom_user", bis_custom_user);
		return "/backstage/cl/wechat/project/subscribeProject";
	}
	
	
	/**
	 * 订单
	 * @param model
	 * @param shop_id
	 * @param session
	 * @return
	 */
	//从订单信息跳转到店铺详情
	@RequestMapping("/GoshowShopDetail")
	public String GoshowShopDetail(Model model,@RequestParam(value="shop_id")String shop_id,HttpSession session){
		Bis_custom_user bis_custom_user=(Bis_custom_user) session.getAttribute("user");
		Bis_shop bis_shop= sus.getBis_shopByid(shop_id);
		Map<String, Object> map=new HashMap<>();
		map.put("shop_id", shop_id);
		List<Bis_project_record> bis_project_records=impl.selectBis_project_record(map);
		if (bis_project_records.size()!=0) {
			model.addAttribute("bis_project_records", bis_project_records);
		}
		model.addAttribute("bis_shop", bis_shop);
		model.addAttribute("bis_custom_user", bis_custom_user);
		return "/backstage/cl/wechat/shop/showShopDetail";
	}
	
	//从订单信息跳转到订单详情
	@RequestMapping("/GoOrder_details")
	public String GoOrder_details(Model model,@RequestParam(value="order_id")String order_id,@RequestParam(value="order_status")String order_status){
		String order=order_status;
		Map<String, Object> map=new HashMap<>();
		map.put("order_id", order_id);
		//订单
		Bis_business_order bis_business_order=impl.getBis_business_orderByid(map);
		model.addAttribute("bis_business_order", bis_business_order);
		//定金表
		Bis_order_deposit bis_order_deposit=impl.getBis_order_depositByid(order_id);
		model.addAttribute("bis_order_deposit", bis_order_deposit);
		model.addAttribute("order", order);
		if (order.equals("1")) {//已预约
			return "/backstage/cl/wechat/my/order/showSubscribeOrderDetail";
		}else if (order.equals("2")) {//服务中
			return "/backstage/cl/wechat/my/order/showServerOrderDetail";
		}else if (order.equals("4")) {//已支付
			return "/backstage/cl/wechat/my/order/showPayOrderDetail";
		}else if (order.equals("7")) {//已撤销
			return "/backstage/cl/wechat/my/order/showCancelOrderDetail";
		}else{
			return "/backstage/cl/wechat/my/order/showCompleteOrderDetail";
		}
		
	}
	

}
